home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / internet-tools / wwwcache2.1 / rexx / cache.www next >
Encoding:
Text File  |  1996-01-15  |  15.2 KB  |  645 lines

  1. /*  $VER: Cache.www 2.1 (15.1.96) Stephan Lichtendahl
  2. Caching of inlined images and HTML documents on harddisk */
  3.  
  4. cachedir='AmiTCP:Cache/'
  5.  
  6. if ~show(l,'rexxsupport.library') then
  7.   if ~addlib('rexxsupport.library',0,-30,0) then
  8.     exit
  9. if ~show(l,'rexxdossupport.library') then
  10.   if ~addlib('rexxdossupport.library',0,-30,2) then
  11.     exit
  12.  
  13. hit=0
  14. zoek=0
  15. nl='0a'x
  16. cr='0d'x
  17. Update=''
  18. Port='AMOSAIC.1'
  19. OPTIONS RESULTS
  20.  
  21. parse arg args
  22. if ~ReadArgs(args,'Save/S,Load/S,Remove/S,Port,Update/K') | Port = '?' then do
  23.   say 'Usage: Cache.www [Save] to get/put images in current AMosaic'
  24.   say 'document from/in cache, Save will save HTML document as well.'
  25.   say 'Remove will remove the current AMosaic document from cache,'
  26.   say 'and every image in it which is currently visable (ie. loaded).'
  27.   say 'Cache.www Load reloads the current cached document from WWW.'
  28.   say 'Cache.www Update <AMosaic program> will update the hotlists'
  29.   say 'env(arc):mosaic/.mosaic-hotlist-default and start AMosaic.'
  30.   return 0
  31. end
  32. if Update ~= '' then do
  33.   if exists(cachedir'DoHTMLlist') then
  34.     call UpdateHL
  35.   address command 'Run >Nil: 'Update
  36.   return 0
  37. end
  38. address value Port
  39.  
  40. if ~Load then do
  41.   'fetch images IMAGE'
  42.   call open(1input,cachedir'ImageList','R')
  43.   list = nl||readch(1input,65534)
  44.   call close(1input)
  45. end
  46.  
  47. if ~Load & ~Remove then do
  48.   do i=1 for image.0
  49.     urlnaam=image.i.url
  50.     av=image.i.current
  51.     nieuw.i=''
  52.  
  53.     if left(urlnaam,17) = 'file://localhost/' then do
  54.       if ~av then
  55.         'fetch inline url 'urlnaam
  56.      end
  57.     else do
  58.       call MkLocal
  59.       nr = pos(nl||uremote||nl,list)
  60.  
  61.       if nr = 0 & av then do
  62.         address command 'Echo >>'cachedir'ImageList "'uremote'"'
  63.         list=list||uremote||nl
  64.         call ChkTree
  65.       end
  66.       if nr > 0 & ~av then do
  67.         nieuw.i=ulocal
  68.         hit=2
  69.       end
  70.       if av then do
  71.         nieuw.i=ulocal
  72.         call open(2input,image.i.filename,'R')
  73.         call open(2output,cachedir||ulocal,'W')
  74.         do until eof(2input)
  75.           call writech(2output,readch(2input,65535))
  76.         end
  77.         call close(2input)
  78.         call close(2output)
  79.       end
  80.     end
  81.  
  82.   end
  83. end
  84.  
  85. if Remove then do
  86.   do i=1 for image.0
  87.     if image.i.current then do
  88.       urlnaam=image.i.url
  89.       if left(urlnaam,17) ~= 'file://localhost/' then
  90.         call MkLocal
  91.       else do
  92.         ulocal = substr(urlnaam,18)
  93.         call MkRemote
  94.       end
  95.       call DelItem
  96.     end
  97.   end
  98.  
  99.   if zoek > 0 then do
  100.     call open(1output,cachedir'Imagelist','W')
  101.     call writech(1output,substr(list,2))
  102.     call close(1output)
  103.   end
  104. end
  105.  
  106. 'get url'
  107. urlnaam = result
  108. if left(urlnaam,17) ~= 'file://localhost/' then
  109.   call MkLocal
  110. else do
  111.   ulocal = substr(urlnaam,18)
  112.   if left(ulocal,10) = 'T:temphtml' then
  113.     uremote=0
  114.   else
  115.     uremote=''
  116. end
  117.  
  118. if uremote = 0 & (Load | Save) then do
  119.   'jump back'
  120.   return 0
  121. end
  122.  
  123. call open(3input,cachedir'HTMLlist','R')
  124. list = nl||readch(3input,65534)
  125. call close(3input)
  126.  
  127. if uremote = '' & Load then do
  128.   call MkRemote
  129.   if uremote ~= '' then
  130.     'jump url http://'uremote
  131. end
  132.  
  133. if Save then do
  134.   if uremote = '' then do
  135.     call MkRemote
  136.     if uremote = '' then do
  137.       address command 'GetString "Local document - change URL for remote links" String "http://'ulocal'" Var newurl Global'
  138.       if RC ~= 0 then
  139.         return 0
  140.       else do
  141.         urlnaam = getvar('newurl')
  142.         call deletevar('newurl')
  143.         call MkLocal
  144.       end
  145.     end
  146.   end
  147.  
  148.   hit=hit+1
  149.   saveto=cachedir||ulocal
  150.  
  151.   nr = pos(nl||uremote||nl,list)
  152.   if nr = 0 then do
  153.     address command 'Echo >>'cachedir'HTMLlist "'uremote'"'
  154.     list=list||uremote||nl
  155.     call ChkTree
  156.  
  157.     'fetch title'
  158.     titel = translate(result,,nl'"'cr)
  159.     address command 'Echo >>'cachedir'Cache.html "<A HREF=*"/'saveto'*">'titel'</A><BR>"'
  160.     address command 'Echo >>'cachedir'DoHTMLlist "+'uremote'"'
  161.  
  162.     zoek=1
  163.     zoek.0=urlnaam
  164.     vervang.0='/'saveto
  165.   end
  166. end
  167.  
  168. if Remove then do
  169.   if uremote ~= 0 & (zoek = 0 | zoek = image.0) then do
  170.     if uremote = '' then
  171.       call MkRemote
  172.     if uremote ~= '' then do
  173.       call DelItem
  174.  
  175.       call open(3output,cachedir'HTMLlist','W')
  176.       call writech(3output,substr(list,2))
  177.       call close(3output)
  178.  
  179.       call open(5input,cachedir'Cache.html','R')
  180.       cachtml = readch(5input,65535)
  181.       call close(5input)
  182.  
  183.       nr = pos('/'cachedir||ulocal'"',cachtml)
  184.       if nr > 0 then do
  185.         startnr = lastpos(nl,cachtml,nr)+1
  186.         stopnr = pos(nl,cachtml,nr)+1
  187.         cachtml = delstr(cachtml,startnr,stopnr-startnr)
  188.  
  189.         call open(5output,cachedir'Cache.html','W')
  190.         call writech(5output,cachtml)
  191.         call close(5output)
  192.       end
  193.  
  194.       address command 'Echo >>'cachedir'DoHTMLlist "-'uremote'"'
  195.       'jump url http://'uremote
  196.     end
  197.   end
  198.  
  199.   if zoek > 0 then
  200.     call MultiFR
  201. end
  202.  
  203. if hit>0 then do
  204.   if hit = 2 then
  205.     do n=1 until ~exists(saveto)
  206.       saveto='T:temphtml'n
  207.     end
  208.  
  209.   'fetch source'
  210.   inhoud = result
  211.   nr = max(pos('<html',inhoud),pos('<HTML',inhoud))
  212.   if nr > 0 then do
  213.     startnr=nr+5
  214.     stopnr = pos('>',inhoud,startnr)
  215.     if startnr ~= stopnr then
  216.       inhoud = delstr(inhoud,startnr,stopnr-startnr)
  217.   end
  218.   if nr > 1 then
  219.     inhoud = substr(inhoud,nr)
  220.  
  221.   urlstam = left(urlnaam,lastpos('/',urlnaam)-1)
  222.   urlroot = left(urlnaam,pos('/',urlnaam,8)-1)
  223.   call ChkLinks
  224.   zkinhoud=''
  225.   info='<A HREF="mailto:s.e.m.lichtendahl@et.tudelft.nl">WWWCache 2.1 © S.Lichtendahl</A>'
  226.   call ChkCache
  227.   if hit=2 then
  228.     inhoud=inhoud'<H6>This is a temporal page created by 'info'</H6>'
  229.  
  230.   call open(4output,saveto,'W')
  231.   call writech(4output,inhoud)
  232.   call close(4output)
  233.   inhoud=''
  234.  
  235.   'jump url file://localhost/'saveto
  236.   do i=1 for image.0
  237.     if nieuw.i ~= '' then
  238.       'fetch inline url /'cachedir||nieuw.i
  239.   end
  240.   if hit = 2 then
  241.     call delete(saveto)
  242.  
  243.   if zoek > 0 then
  244.     call MultiFR
  245. end
  246. exit 0
  247.  
  248. MkLocal:
  249.   nr = pos('://',urlnaam)+3
  250.   if nr = 3 then
  251.     nr = pos(':',urlnaam)+1
  252.   uremote = substr(urlnaam,nr)
  253.  
  254.   zoeknr = pos(':',uremote)
  255.   if zoeknr > 0 then
  256.     if left(uremote,10) = 'localhost/' then
  257.       ulocal = substr(uremote,zoeknr+1)
  258.     else do
  259.       midnr = pos('/',uremote,zoeknr+1)
  260.       ulocal = delstr(uremote,zoeknr,midnr-zoeknr)
  261.     end
  262.   else
  263.     ulocal=uremote
  264.  
  265.   if right(ulocal,1) = '/' then
  266.     ulocal=ulocal'.html'
  267. return 0
  268.  
  269. MkRemote:
  270.   if left(ulocal,length(cachedir)) ~= cachedir then
  271.     uremote=''
  272.   else do
  273.     ulocal = substr(ulocal,length(cachedir)+1)
  274.     if right(ulocal,6) = '/.html' then
  275.       uremote = left(ulocal,length(ulocal)-5)
  276.     else
  277.       uremote=ulocal
  278.  
  279.     if pos(nl||uremote||nl,list) = 0 then do
  280.       stopnr=0
  281.       nr = pos(':'uremote||nl,list)
  282.       if nr > 0 then do
  283.         startnr = lastpos(nl,list,nr)+1
  284.         stopnr = pos(nl,list,nr)
  285.  
  286.        end
  287.       else do
  288.         nr = pos('/',uremote)
  289.         uleft = left(uremote,nr-1)':'
  290.         uright = substr(uremote,nr)nl
  291.  
  292.         nr = pos(uright,list)
  293.         do while nr > 0 & stopnr = 0
  294.           startnr = lastpos(nl,list,nr)+1
  295.           zoeknr = pos(nl,list,nr)
  296.           if substr(list,startnr,length(uleft)) = uleft then
  297.             stopnr=zoeknr
  298.  
  299.           nr = pos(uright,list,zoeknr+1)
  300.         end
  301.       end
  302.  
  303.       if stopnr > 0 then 
  304.         uremote = substr(list,startnr,stopnr-startnr)
  305.       else
  306.         uremote=''
  307.     end
  308.  
  309.   end
  310. return 0
  311.  
  312. ChkTree:
  313.   zoeknr = pos('/',ulocal)
  314.   do while zoeknr > 0
  315.     pad = cachedir||left(ulocal,zoeknr-1)
  316.     if ~exists(pad) then
  317.       call makedir(pad)
  318.  
  319.     zoeknr = pos('/',ulocal,zoeknr+1)
  320.   end
  321. return 0
  322.  
  323. DelItem:
  324.   nr = pos(nl||uremote||nl,list)
  325.   if nr > 0 then do
  326.     list = delstr(list,nr+1,length(uremote)+1)
  327.  
  328.     zoeknr = lastpos('/',ulocal'/')
  329.     do until zoeknr = 0 | nr = 0
  330.       pad = cachedir||left(ulocal,zoeknr-1)
  331.       nr = delete(pad)
  332.       if nr then
  333.         say pad 'deleted.'
  334.  
  335.       zoeknr = lastpos('/',ulocal,zoeknr-1)
  336.     end
  337.  
  338.     zoek.zoek='/'cachedir||ulocal
  339.     vervang.zoek='http://'uremote
  340.     zoek=zoek+1
  341.   end
  342. return 0
  343.  
  344. ChkLinks:
  345.   delta=0
  346.   zkinhoud = upper(inhoud)
  347.   zoeknr = pos('<A',zkinhoud)
  348.   do while zoeknr > 0
  349.  
  350.     zoeknr = verify(zkinhoud,nl cr,,zoeknr+2)
  351.     if substr(zkinhoud,zoeknr,4) ~= 'HREF' then
  352.       zklen=1
  353.     else do
  354.       zoeknr = verify(zkinhoud,nl cr,,zoeknr+4)
  355.       if substr(zkinhoud,zoeknr,1) ~= '=' then
  356.         zklen=1
  357.       else do
  358.         zoeknr = verify(zkinhoud,nl cr,,zoeknr+1)
  359.  
  360.         if substr(zkinhoud,zoeknr,1) = '"' then
  361.           zoeknr=zoeknr+1
  362.         else do
  363.           inhoud = insert('"',inhoud,zoeknr+delta-1)
  364.           delta=delta+1
  365.         end
  366.         stopnr = pos('>',zkinhoud,zoeknr)
  367.         zklen=stopnr-zoeknr
  368.  
  369.         urlnaam = substr(zkinhoud,zoeknr,zklen)
  370.         if pos('://',urlnaam) = 0 then
  371.           call ReplUrl
  372.         if right(urlnaam,1) ~= '"' then do
  373.           inhoud = insert('"',inhoud,stopnr+delta-1)
  374.           delta=delta+1
  375.         end
  376.  
  377.       end
  378.     end
  379.     zoeknr = pos('<A',zkinhoud,zoeknr+zklen)
  380.   end
  381.  
  382.   delta=0
  383.   zkinhoud = upper(inhoud)
  384.   zoeknr = pos('SRC',zkinhoud)
  385.   do while zoeknr > 0
  386.  
  387.     nr = verify(zkinhoud,nl cr,'M',zoeknr-1)
  388.     if nr ~= zoeknr-1 then
  389.       zklen=1
  390.     else do
  391.       zoeknr = verify(zkinhoud,nl cr,,zoeknr+3)
  392.       if substr(zkinhoud,zoeknr,1) ~= '=' then
  393.         zklen=1
  394.       else do
  395.         zoeknr = verify(zkinhoud,nl cr,,zoeknr+1)
  396.  
  397.         if substr(zkinhoud,zoeknr,1) = '"' then
  398.           zoeknr=zoeknr+1
  399.         else do
  400.           inhoud = insert('"',inhoud,zoeknr+delta-1)
  401.           delta=delta+1
  402.         end
  403.         stopnr = verify(zkinhoud,'">'nl cr,'M',zoeknr)+1
  404.         zklen=stopnr-zoeknr
  405.  
  406.         urlnaam = substr(zkinhoud,zoeknr,zklen)
  407.         if pos('://',urlnaam) = 0 then
  408.           call ReplUrl
  409.         if right(urlnaam,1) ~= '"' then do
  410.           inhoud = insert('"',inhoud,stopnr+delta-2)
  411.           delta=delta+1
  412.         end
  413.  
  414.       end
  415.     end
  416.     zoeknr = pos('SRC',zkinhoud,zoeknr+zklen)
  417.   end
  418.  
  419.   delta=0
  420.   zkinhoud = upper(inhoud)
  421.   zoeknr = pos('ACTION',zkinhoud)
  422.   do while zoeknr > 0
  423.  
  424.     zoeknr = verify(zkinhoud,nl cr,,zoeknr+6)
  425.     if substr(zkinhoud,zoeknr,1) ~= '=' then
  426.       zklen=1
  427.     else do
  428.       zoeknr = verify(zkinhoud,'"'nl cr,,zoeknr+1)
  429.       stopnr = pos('"',zkinhoud,zoeknr)
  430.       zklen=stopnr-zoeknr
  431.  
  432.       urlnaam = substr(zkinhoud,zoeknr,zklen)
  433.       if pos('://',urlnaam) = 0 then
  434.         call ReplUrl
  435.     end
  436.     zoeknr = pos('ACTION',zkinhoud,zoeknr+zklen)
  437.   end
  438. return 0
  439.  
  440. ReplUrl:
  441.   nr = pos(':',urlnaam)
  442.   if nr>0 then
  443.     if left(urlnaam,5) = 'HTTP:' then do
  444.       urlnaam = substr(urlnaam,6)
  445.       inhoud = delstr(inhoud,zoeknr+delta,5)
  446.       zoeknr=zoeknr+5
  447.       delta=delta-5
  448.       nr=0
  449.     end
  450.  
  451.   if nr=0 then
  452.     if left(urlnaam,1) ~= '#' then do
  453.       if left(urlnaam,1) = '/' then
  454.         tekst=urlroot
  455.       else do
  456.         tekst=urlstam
  457.  
  458.         do while left(urlnaam,3) = '../'
  459.           urlnaam = substr(urlnaam,4)
  460.           inhoud = delstr(inhoud,zoeknr+delta,3)
  461.           zoeknr=zoeknr+3
  462.           delta=delta-3
  463.           tekst = left(tekst,lastpos('/',tekst)-1)
  464.         end
  465.  
  466.         tekst=tekst'/'
  467.       end
  468.  
  469.       inhoud = insert(tekst,inhoud,zoeknr+delta-1)
  470.       delta=delta+length(tekst)
  471.     end
  472. return 0
  473.  
  474. ChkCache:
  475.   do i=1 for image.0
  476.     if nieuw.i ~= '' then do
  477.       nr = pos(image.i.url'"',inhoud)
  478.       if nr = 0 then
  479.         inhoud=inhoud'<H6>Error: Image URL 'image.i.url' not found by 'info' Please mail URL of this page</H6>'
  480.       else do
  481.  
  482.         inhoud = delstr(inhoud,nr,length(image.i.url))
  483.         inhoud = insert('/'cachedir||nieuw.i,inhoud,nr-1)
  484.       end
  485.     end
  486.   end
  487.  
  488.   if hit ~= 2 then do
  489.     'fetch links HREF'
  490.     do i=1 for href.0
  491.       urlnaam=href.i.url
  492.       if left(urlnaam,17) ~= 'file://localhost/' then do
  493.         call MkLocal
  494.         nr = pos(nl||uremote||nl,list)
  495.         if nr > 0 then do
  496.  
  497.           nr = pos(urlnaam,inhoud)
  498.           do while nr > 0
  499.             zklen=length(urlnaam)
  500.             tekst=substr(inhoud,nr+zklen,1)
  501.  
  502.             if tekst='#' | tekst='"' then
  503.               if pos('ACTION',upper(substr(inhoud,nr-11,10))) = 0 then do
  504.  
  505.                 inhoud = delstr(inhoud,nr,zklen)
  506.                 inhoud = insert('/'cachedir||ulocal,inhoud,nr-1)
  507.               end
  508.  
  509.             nr = pos(urlnaam,inhoud,nr+zklen)
  510.           end
  511.  
  512.         end
  513.       end
  514.     end
  515.  
  516.   end
  517. return 0
  518.  
  519. MultiFR:
  520.   startnr=2
  521.   stopnr = pos(nl,list,2)
  522.   do while stopnr > 0
  523.     urlnaam = 'http://'substr(list,startnr,stopnr-startnr)
  524.     call MkLocal
  525.  
  526.     q=0
  527.     call open(6input,cachedir||ulocal,'R')
  528.     inhoud = readch(6input,65535)
  529.     call close(6input)
  530.  
  531.     do i=0 for zoek
  532.       nr = pos(zoek.i,inhoud)
  533.       do while nr > 0
  534.         zklen=length(zoek.i)
  535.         tekst=substr(inhoud,nr+zklen,1)
  536.  
  537.         if tekst='#' | tekst='"' then
  538.           if pos('ACTION',upper(substr(inhoud,nr-11,10))) = 0 then do
  539.             q=1
  540.             inhoud = delstr(inhoud,nr,zklen)
  541.             inhoud = insert(vervang.i,inhoud,nr-1)
  542.             zklen=length(vervang.i)
  543.           end
  544.  
  545.         nr = pos(zoek.i,inhoud,nr+zklen)
  546.       end
  547.     end                                                         
  548.  
  549.     if q then do
  550.       call open(6output,cachedir||ulocal,'W')
  551.       call writech(6output,inhoud)
  552.       call close(6output)
  553.       say cachedir||ulocal 'updated.'
  554.     end
  555.  
  556.     startnr=stopnr+1
  557.     stopnr = pos(nl,list,startnr)
  558.   end
  559. return 0
  560.  
  561. UpdateHL:
  562.   call open(3input,cachedir'DoHTMLlist','R')
  563.   list = readch(3input,65535)
  564.   call close(3input)
  565.  
  566.   call open(7input,'Env:Mosaic/.mosaic-hotlist-default','R')
  567.   inhoud = readch(7input,65535)
  568.   call close(7input)
  569.  
  570.   cachtml=''
  571.   startnr=1
  572.   stopnr = pos(nl,list)
  573.   do while stopnr > 0
  574.     tekst = substr(list,startnr,1)
  575.     urlnaam = 'http://'substr(list,startnr+1,stopnr-startnr-1)
  576.     call MkLocal
  577.  
  578.     if tekst = '-' then do
  579.       zoeknr = pos('file://localhost/'cachedir||ulocal,inhoud)
  580.       if zoeknr > 0 then do
  581.         nr = pos(nl,inhoud,zoeknr+17)+2
  582.         inhoud = delstr(inhoud,zoeknr,nr-zoeknr)
  583.         zoeknr=zoeknr-1
  584.         urlnaam='http://'uremote
  585.       end
  586.     end
  587.     if tekst = '+' then do
  588.       if pos('/'cachedir||ulocal,inhoud) > 0 then
  589.         zoeknr=0
  590.       else do
  591.         urlnaam='file://localhost/'cachedir||ulocal
  592.  
  593.         zoeknr = pos('http://'uremote,inhoud)
  594.         if zoeknr > 0 then do
  595.           nr = pos(nl,inhoud,zoeknr+7)+1
  596.           inhoud = delstr(inhoud,zoeknr,nr-zoeknr)
  597.           zoeknr=zoeknr-1
  598.           inhoud = insert('©',inhoud,zoeknr)
  599.  
  600.          end
  601.         else do
  602.           zoeknr = pos('Default',inhoud)+7
  603.  
  604.           if cachtml = '' then do
  605.             call open(5input,cachedir'Cache.html','R')
  606.             cachtml = readch(5input,65535)
  607.             call close(5input)
  608.           end
  609.  
  610.           tekst='/'cachedir||ulocal'">'
  611.           midnr = pos(tekst,cachtml)
  612.           if midnr = 0 then
  613.             titel = 'URL: http://'uremote
  614.           else do
  615.             midnr=midnr+length(tekst)
  616.             nr = pos('<',cachtml,midnr)
  617.             titel = strip(substr(cachtml,midnr,nr-midnr))
  618.           end
  619.           inhoud = insert('©'titel||nl,inhoud,zoeknr)
  620.         end
  621.  
  622.       end
  623.     end
  624.  
  625.     if zoeknr > 0 then do
  626.       parse value date() with datum maand jaar .
  627.       dag = left(date('W'),3)
  628.       inhoud = insert(urlnaam dag maand datum time() jaar||nl,inhoud,zoeknr)
  629.     end
  630.  
  631.     startnr=stopnr+1
  632.     stopnr = pos(nl,list,startnr)
  633.   end
  634.  
  635.   call delete(cachedir'DoHTMLlist')
  636.  
  637.   call open(7output,'Env:Mosaic/.mosaic-hotlist-default','W')
  638.   call writech(7output,inhoud)
  639.   call close(7output)
  640.   call open(7output,'EnvArc:Mosaic/.mosaic-hotlist-default','W')
  641.   call writech(7output,inhoud)
  642.   call close(7output)
  643. return 0
  644.  
  645.